import baseUrl from './config.js'
// 修改图片地址为oss地址
export const getOssUrl = (url) => {
	if (url.startsWith(baseUrl))
		return url.replace(baseUrl, 'https://xzcs-video.oss-cn-beijing.aliyuncs.com')

	if (url.startsWith(baseUrl))
		return url.replace(baseUrl, 'https://xzcs-video.oss-cn-beijing.aliyuncs.com')

	else if (url.startsWith(baseUrl))
		return url.replace(baseUrl, 'https://xzcs-video.oss-cn-beijing.aliyuncs.com')

	else if (url.startsWith('http://resources.xiaozhucaishui.com'))
		return url.replace('http://resources.xiaozhucaishui.com', 'https://xzcs-video.oss-cn-beijing.aliyuncs.com')

	return url

};

// 相对路径地址拼接为oss绝对地址
export const getRelativeOss = (url) => {

	return 'https://xzcs-video.oss-cn-beijing.aliyuncs.com' + url

};



// 文字高亮 keyword 需要高亮的文字
export function hightLightText(text, keyword) {
	let str = text
	if (!str) return ''
	for (let key of keyword) {
		if (!hasSpecialStr(key)) {
			const reg = new RegExp(key, 'gi') // 动态正则表达式
			str = str.replace(reg, `<text style="color:red">${key}</text>`) // 使用replace替换
		}
	}
	return str
};

//毫秒数转年月日，月日小于10补0用-分割
export const formatTime = (time) => {
	var date = new Date(time)
	var year = date.getFullYear()
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
	return year + '-' + month + '-' + day
}

// 年月日
export function parseTimeDot(time, cFormat) {
	if (arguments.length === 0) {
		return null
	}
	const format = cFormat || '{y}年{m}月{d}日'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if (('' + time).length === 10) time = parseInt(time) * 1000
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}


//聊天时，发送时间处理
export function formatSendTime(e) {
	let old = new Date(e);
	let now = new Date();
	//获取old具体时间
	let d = old.getTime();
	let h = old.getHours();
	let m = old.getMinutes();
	let Y = old.getFullYear();
	let M = old.getMonth() + 1;
	let D = old.getDate();
	//获取now具体时间
	let nd = now.getTime();
	let nh = now.getHours();
	let n = now.getMinutes();
	let nY = now.getFullYear();
	let nM = now.getMonth() + 1;
	let nD = now.getDate();

	//当天的时间
	if (D === nD && M === nM && Y === nY) {
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		return h + ':' + m;
	}
	//昨天时间
	if (D + 1 === nD && M === nM && Y === nY) {
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		return '昨天 ' + h + ':' + m;
	} else if (Y == nY) {
		//今年
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		return M + '月' + D + '日 ' + h + ':' + m
	} else {
		//大于今年
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		return Y + '年' + M + '月' + D + '日 ' + h + ':' + m
	}
}


// JS判断字符串是否含有特殊符号，包括英文与中文两种输入模式。
export function hasSpecialStr(str) {
	var specialChars = "~·`!！@#$￥%^…&*()（）—-_=+[]{}【】、|\\;:；：'\"“‘,./<>《》?？，。emEM";
	if (specialChars.indexOf(str) != -1) return true
	return false;
};